package com.tengju.bff.interfaces.app.domination.assembler;

import com.tengju.bff.interfaces.app.domination.vo.OwnerLeaderVO;
import com.tengju.bff.interfaces.manage.crm.user.vo.NicknameVO;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.UserInfo;

import java.util.Optional;

public class OwnerLeaderAssembler {

    public static OwnerLeaderVO assemblerToVO(StarLevelEnum starLevel,UserInfo operator,UserInfo serviceProvider,UserInfo parent) {
        String operatorName = null;
        String serviceProviderName = null;
        String parentName = null;

        if (starLevel.isServiceProvider(starLevel)) {
            if (operator != null) {
                operatorName = convertName(operator);
            }
        }

        if (starLevel.isDealer(starLevel)) {
            if (operator != null) {
                operatorName = convertName(operator);
            }
            if (serviceProvider != null) {
                serviceProviderName = convertName(serviceProvider);
            }
            if (parent != null) {
                parentName = convertName(parent);
            }

        }

        return new OwnerLeaderVO(operatorName,serviceProviderName,parentName);

    }

    private static String convertName(UserInfo operator) {
        String operatorName;
        operatorName = new NicknameVO(Optional.of(operator).map(UserInfo::getNickName).orElse(null),
                Optional.of(operator).map(UserInfo::getWholePhoneNumber).orElse(null)).toString()
                + " " + Optional.of(operator).map(UserInfo::starLevel)
                .map(StarLevelEnum::getShortName).orElse("");
        return operatorName;
    }
}
